﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
namespace Exhibition.Dao
{

    public class StandDao
    {


        public StandDao()
        {

            Mapper.CreateMap<E_Stand, StandModel>();
        }
        public List<E_Stand> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_Stand.Count();

                return edm.E_Stand.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public List<StandModel> SearchStand(string key)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                var query = edm.E_Stand.Where(s => s.StandNum.IndexOf(key) >= 0);
                return Mapper.Map<List<E_Stand>, List<StandModel>>(query.ToList());


            }
        }


        public StandModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Stand.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<E_Stand, StandModel>(query);
            }

        }


        public int Save(StandModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Stand.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_Stand();

                    edm.AddToE_Stand(query);

                }


                query.StandNum = obj.StandNum;

                query.DisplayNum = obj.DisplayNum;

                query.ClassId = obj.ClassId;

                query.StandLen = obj.StandLen;

                query.StandWidth = obj.StandWidth;

                query.StandSize = obj.StandSize;

                query.DisplayMsg = obj.DisplayMsg;
                query.CreateUserId = obj.CreateUserId;
                query.ColorTpId = obj.ColorTpId;
                query.StandPoint = obj.Points;
                edm.SaveChanges();

                return query.Id;


            }

        }


        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Stand.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

